package main.katalog;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import main.katalog.model.users;

/**
 * Servlet implementation class login
 */
@WebServlet("/login")

public class login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public login() {
        super();
        // TODO Auto-generated constructor stub 
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		int userID=-1;
		HttpSession session = request.getSession();

		if(request.getParameter("button").equals("Login")){	
			users user = new users();
			try {
				userID=user.getUserID(username, password);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(userID);
			if(userID == -1){
				request.setAttribute("invalidlogin", true);
				request.getRequestDispatcher("/index.jsp").forward(request,response);
			}
			else{
				request.setAttribute("invalidlogin", false);
				session.setAttribute("username", username);
				session.setAttribute("userID", userID);
				request.getRequestDispatcher("/welcome.jsp").forward(request,response);
			}
		}
		else if(request.getParameter("buton").equals("Şifre Değiştir")){
			
		}
	}
	

}
